package com.wangyadong.hobby.schedule.jobtask.jvm;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/11/208:37
 *
 * 回收方法区：
 * -verbose:class           查询类加载和卸载类信息
 * -XX:+TraceClassUnloading 查询类加载和卸载类信息 product版虚拟机中
 **/
public class FinalizeEscapeGC {

    private static FinalizeEscapeGC SAVE_HOOK = null;

    public void isAlive() {
        System.out.println("yes iam still alive");
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize method executed");
        FinalizeEscapeGC.SAVE_HOOK = this;
    }

    public static void main(String[] args) {

        SAVE_HOOK = new FinalizeEscapeGC();
        SAVE_HOOK=null;
        System.gc();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (SAVE_HOOK != null) {
            SAVE_HOOK.isAlive();
        } else {
            System.out.println("no i am dead ");
        }
        /////
        SAVE_HOOK=null;
        System.gc();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (SAVE_HOOK != null) {
            SAVE_HOOK.isAlive();
        } else {
            System.out.println("no i am dead ");
        }
    }
}
